<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>BIP.SMC.condensation</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="BIP-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://code.google.com/p/bayesian-inference">Project Homepage</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="BIP-module.html">Package&nbsp;BIP</a> ::
        <a href="BIP.SMC-module.html">Package&nbsp;SMC</a> ::
        Module&nbsp;condensation
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
    onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="BIP.SMC.condensation-pysrc.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<h1 class="epydoc">Source Code for <a href="BIP.SMC.condensation-module.html">Module BIP.SMC.condensation</a></h1>
<pre class="py-src">
<a name="L1"></a><tt class="py-lineno">  1</tt>  <tt class="py-line"><tt class="py-comment"># -*- coding:utf-8 -*-</tt> </tt>
<a name="L2"></a><tt class="py-lineno">  2</tt>  <tt class="py-line"><tt class="py-comment">#-----------------------------------------------------------------------------</tt> </tt>
<a name="L3"></a><tt class="py-lineno">  3</tt>  <tt class="py-line"><tt class="py-comment"># Name:        condensation.py</tt> </tt>
<a name="L4"></a><tt class="py-lineno">  4</tt>  <tt class="py-line"><tt class="py-comment"># Project:  Bayesian-Inference</tt> </tt>
<a name="L5"></a><tt class="py-lineno">  5</tt>  <tt class="py-line"><tt class="py-comment"># Purpose:     implementation of the Condensation algorithm, originally conceived by Michael Isard</tt> </tt>
<a name="L6"></a><tt class="py-lineno">  6</tt>  <tt class="py-line"><tt class="py-comment">#                   http://www.cs.ubc.ca/~nando/smc/index.htm</tt> </tt>
<a name="L7"></a><tt class="py-lineno">  7</tt>  <tt class="py-line"><tt class="py-comment">#</tt> </tt>
<a name="L8"></a><tt class="py-lineno">  8</tt>  <tt class="py-line"><tt class="py-comment"># Author:      Fl&#225;vio Code&#231;o Coelho&lt;fccoelho@gmail.com&gt;</tt> </tt>
<a name="L9"></a><tt class="py-lineno">  9</tt>  <tt class="py-line"><tt class="py-comment">#</tt> </tt>
<a name="L10"></a><tt class="py-lineno"> 10</tt>  <tt class="py-line"><tt class="py-comment"># Created:     2008-11-26</tt> </tt>
<a name="L11"></a><tt class="py-lineno"> 11</tt>  <tt class="py-line"><tt class="py-comment"># Copyright:   (c) 2008 by the Author</tt> </tt>
<a name="L12"></a><tt class="py-lineno"> 12</tt>  <tt class="py-line"><tt class="py-comment"># Licence:     GPL</tt> </tt>
<a name="L13"></a><tt class="py-lineno"> 13</tt>  <tt class="py-line"><tt class="py-comment">#-----------------------------------------------------------------------------</tt> </tt>
<a name="L14"></a><tt class="py-lineno"> 14</tt>  <tt class="py-line"><tt class="py-name">__docformat__</tt> <tt class="py-op">=</tt> <tt class="py-docstring">"restructuredtext en"</tt> </tt>
<a name="L15"></a><tt class="py-lineno"> 15</tt>  <tt class="py-line"><tt class="py-string">'''</tt> </tt>
<a name="L16"></a><tt class="py-lineno"> 16</tt>  <tt class="py-line"><tt class="py-string">implementation of the Condensation algorithm, originally conceived by Michael Isard</tt> </tt>
<a name="L17"></a><tt class="py-lineno"> 17</tt>  <tt class="py-line"><tt class="py-string">http://www.cs.ubc.ca/~nando/smc/index.htm</tt> </tt>
<a name="L18"></a><tt class="py-lineno"> 18</tt>  <tt class="py-line"><tt class="py-string">'''</tt> </tt>
<a name="L19"></a><tt class="py-lineno"> 19</tt>  <tt class="py-line"> </tt>
<a name="L20"></a><tt class="py-lineno"> 20</tt>  <tt class="py-line"><tt class="py-keyword">from</tt> <tt class="py-name">numpy</tt><tt class="py-op">.</tt><tt class="py-name">random</tt> <tt class="py-keyword">import</tt> <tt class="py-name">uniform</tt><tt class="py-op">,</tt> <tt class="py-name">normal</tt><tt class="py-op">,</tt>  <tt class="py-name">random</tt> </tt>
<a name="L21"></a><tt class="py-lineno"> 21</tt>  <tt class="py-line"><tt class="py-keyword">from</tt> <tt class="py-name">numpy</tt> <tt class="py-keyword">import</tt>  <tt class="py-name">array</tt><tt class="py-op">,</tt> <tt class="py-name">ndarray</tt><tt class="py-op">,</tt>  <tt class="py-name">zeros</tt><tt class="py-op">,</tt> <tt class="py-name">nan_to_num</tt> </tt>
<a name="L22"></a><tt class="py-lineno"> 22</tt>  <tt class="py-line"><tt class="py-keyword">from</tt> <tt class="py-name">math</tt> <tt class="py-keyword">import</tt> <tt class="py-name">pi</tt><tt class="py-op">,</tt> <tt class="py-name">exp</tt><tt class="py-op">,</tt> <tt class="py-name">sqrt</tt> </tt>
<a name="L23"></a><tt class="py-lineno"> 23</tt>  <tt class="py-line"><tt class="py-keyword">import</tt> <tt class="py-name">pylab</tt> <tt class="py-keyword">as</tt> <tt class="py-name">P</tt> </tt>
<a name="L24"></a><tt class="py-lineno"> 24</tt>  <tt class="py-line"> </tt>
<a name="Model"></a><div id="Model-def"><a name="L25"></a><tt class="py-lineno"> 25</tt> <a class="py-toggle" href="#" id="Model-toggle" onclick="return toggle('Model');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html">Model</a><tt class="py-op">(</tt><tt class="py-base-class">object</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="Model-expanded"><a name="L26"></a><tt class="py-lineno"> 26</tt>  <tt class="py-line">    <tt class="py-docstring">"""</tt> </tt>
<a name="L27"></a><tt class="py-lineno"> 27</tt>  <tt class="py-line"><tt class="py-docstring">    Example Model Specification</tt> </tt>
<a name="L28"></a><tt class="py-lineno"> 28</tt>  <tt class="py-line"><tt class="py-docstring">    """</tt> </tt>
<a name="Model.__init__"></a><div id="Model.__init__-def"><a name="L29"></a><tt class="py-lineno"> 29</tt> <a class="py-toggle" href="#" id="Model.__init__-toggle" onclick="return toggle('Model.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.__init__-expanded"><a name="L30"></a><tt class="py-lineno"> 30</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt> <tt class="py-op">=</tt> <tt id="link-0" class="py-name" targets="Class BIP.SMC.condensation.GlobalData=BIP.SMC.condensation.GlobalData-class.html"><a title="BIP.SMC.condensation.GlobalData" class="py-name" href="#" onclick="return doclink('link-0', 'GlobalData', 'link-0');">GlobalData</a></tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-number">0</tt><tt class="py-op">,</tt> <tt class="py-number">.2</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-op">(</tt><tt class="py-op">-</tt><tt class="py-number">.1</tt><tt class="py-op">,</tt> <tt class="py-number">.4</tt><tt class="py-op">,</tt> <tt class="py-number">.075</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-op">(</tt><tt class="py-op">-</tt><tt class="py-number">.1</tt><tt class="py-op">,</tt> <tt class="py-number">.4</tt><tt class="py-op">,</tt> <tt class="py-number">.075</tt><tt class="py-op">,</tt> <tt class="py-number">.03</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-number">.03</tt><tt class="py-op">,</tt> <tt class="py-number">1000</tt><tt class="py-op">,</tt> <tt class="py-number">100</tt><tt class="py-op">)</tt> </tt>
<a name="L31"></a><tt class="py-lineno"> 31</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt> <tt class="py-op">=</tt> <tt id="link-1" class="py-name" targets="Class BIP.SMC.condensation.IterationData=BIP.SMC.condensation.IterationData-class.html"><a title="BIP.SMC.condensation.IterationData" class="py-name" href="#" onclick="return doclink('link-1', 'IterationData', 'link-1');">IterationData</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L32"></a><tt class="py-lineno"> 32</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">out</tt><tt class="py-op">=</tt> <tt class="py-name">zeros</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-number">100</tt><tt class="py-op">,</tt> <tt class="py-number">3</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">dtype</tt> <tt class="py-op">=</tt> <tt class="py-name">float</tt><tt class="py-op">)</tt> </tt>
</div><a name="Model.setupPriorConditions"></a><div id="Model.setupPriorConditions-def"><a name="L33"></a><tt class="py-lineno"> 33</tt> <a class="py-toggle" href="#" id="Model.setupPriorConditions-toggle" onclick="return toggle('Model.setupPriorConditions');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#setupPriorConditions">setupPriorConditions</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.setupPriorConditions-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.setupPriorConditions-expanded"><a name="L34"></a><tt class="py-lineno"> 34</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">n</tt> <tt class="py-keyword">in</tt> <tt class="py-name">xrange</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L35"></a><tt class="py-lineno"> 35</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">oldpos</tt><tt class="py-op">[</tt><tt class="py-name">n</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">PriorModel</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt> <tt class="py-op">+</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">PriorModel</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">*</tt><tt class="py-name">normal</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L36"></a><tt class="py-lineno"> 36</tt>  <tt class="py-line">            <tt class="py-comment"># The probabilities are not normalised. </tt> </tt>
<a name="L37"></a><tt class="py-lineno"> 37</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">cumul_prob_array</tt><tt class="py-op">[</tt><tt class="py-name">n</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt>  <tt class="py-name">float</tt><tt class="py-op">(</tt><tt class="py-name">n</tt><tt class="py-op">)</tt> </tt>
<a name="L38"></a><tt class="py-lineno"> 38</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">sample_weights</tt><tt class="py-op">[</tt><tt class="py-name">n</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-number">1.0</tt> </tt>
<a name="L39"></a><tt class="py-lineno"> 39</tt>  <tt class="py-line">        <tt class="py-comment"># The probabilities are not normalised, so store the largest value here </tt> </tt>
<a name="L40"></a><tt class="py-lineno"> 40</tt>  <tt class="py-line">        <tt class="py-comment"># (for simplicity of the binary search algorithm,</tt> </tt>
<a name="L41"></a><tt class="py-lineno"> 41</tt>  <tt class="py-line">        <tt class="py-comment"># cumul_prob_array[0] = 0). This can then be used as a</tt> </tt>
<a name="L42"></a><tt class="py-lineno"> 42</tt>  <tt class="py-line">        <tt class="py-comment"># multiplicative normalisation constant for the sample_weights</tt> </tt>
<a name="L43"></a><tt class="py-lineno"> 43</tt>  <tt class="py-line">        <tt class="py-comment"># array, as well. </tt> </tt>
<a name="L44"></a><tt class="py-lineno"> 44</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">largest_cumulative_prob</tt> <tt class="py-op">=</tt> <tt class="py-name">float</tt><tt class="py-op">(</tt><tt class="py-name">n</tt><tt class="py-op">)</tt> </tt>
<a name="L45"></a><tt class="py-lineno"> 45</tt>  <tt class="py-line">        <tt class="py-comment"># This is the initial positions the simulated object. </tt> </tt>
<a name="L46"></a><tt class="py-lineno"> 46</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-number">0.0</tt><tt class="py-op">;</tt> </tt>
</div><a name="Model.iterate"></a><div id="Model.iterate-def"><a name="L47"></a><tt class="py-lineno"> 47</tt> <a class="py-toggle" href="#" id="Model.iterate-toggle" onclick="return toggle('Model.iterate');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#iterate">iterate</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">previous</tt><tt class="py-op">,</tt> <tt class="py-param">process</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.iterate-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.iterate-expanded"><a name="L48"></a><tt class="py-lineno"> 48</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L49"></a><tt class="py-lineno"> 49</tt>  <tt class="py-line"><tt class="py-docstring">        The process model for a first-order auto-regressive process is:</tt> </tt>
<a name="L50"></a><tt class="py-lineno"> 50</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L51"></a><tt class="py-lineno"> 51</tt>  <tt class="py-line"><tt class="py-docstring">        x_{t+1} - mean = (x_t - mean)*scaling + sigma*w_t</tt> </tt>
<a name="L52"></a><tt class="py-lineno"> 52</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L53"></a><tt class="py-lineno"> 53</tt>  <tt class="py-line"><tt class="py-docstring">        where w_t is unit iid Gaussian noise.</tt> </tt>
<a name="L54"></a><tt class="py-lineno"> 54</tt>  <tt class="py-line"><tt class="py-docstring">        </tt> </tt>
<a name="L55"></a><tt class="py-lineno"> 55</tt>  <tt class="py-line"><tt class="py-docstring">        :Parameters:</tt> </tt>
<a name="L56"></a><tt class="py-lineno"> 56</tt>  <tt class="py-line"><tt class="py-docstring">            -  `previous`: previous data</tt> </tt>
<a name="L57"></a><tt class="py-lineno"> 57</tt>  <tt class="py-line"><tt class="py-docstring">            -  `process`: processmodel parameter tuple</tt> </tt>
<a name="L58"></a><tt class="py-lineno"> 58</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L59"></a><tt class="py-lineno"> 59</tt>  <tt class="py-line">        <tt class="py-keyword">return</tt> <tt class="py-name">process</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">+</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">previous</tt><tt class="py-op">-</tt><tt class="py-name">process</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">*</tt><tt class="py-name">process</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">+</tt><tt class="py-name">process</tt><tt class="py-op">[</tt><tt class="py-number">2</tt><tt class="py-op">]</tt><tt class="py-op">*</tt><tt class="py-name">normal</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
</div><a name="L60"></a><tt class="py-lineno"> 60</tt>  <tt class="py-line">         </tt>
<a name="Model.predictSamplePosition"></a><div id="Model.predictSamplePosition-def"><a name="L61"></a><tt class="py-lineno"> 61</tt> <a class="py-toggle" href="#" id="Model.predictSamplePosition-toggle" onclick="return toggle('Model.predictSamplePosition');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#predictSamplePosition">predictSamplePosition</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">new_sample</tt><tt class="py-op">,</tt> <tt class="py-param">old_sample</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.predictSamplePosition-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.predictSamplePosition-expanded"><a name="L62"></a><tt class="py-lineno"> 62</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L63"></a><tt class="py-lineno"> 63</tt>  <tt class="py-line"><tt class="py-docstring">        This routine samples from the distribution</tt> </tt>
<a name="L64"></a><tt class="py-lineno"> 64</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L65"></a><tt class="py-lineno"> 65</tt>  <tt class="py-line"><tt class="py-docstring">        p(x_t | x_{t-1} = oldpos[old_sample])</tt> </tt>
<a name="L66"></a><tt class="py-lineno"> 66</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L67"></a><tt class="py-lineno"> 67</tt>  <tt class="py-line"><tt class="py-docstring">        and stores the result in new_positions[new_sample]. This is</tt> </tt>
<a name="L68"></a><tt class="py-lineno"> 68</tt>  <tt class="py-line"><tt class="py-docstring">        straightforward for the simple first-order auto-regressive process</tt> </tt>
<a name="L69"></a><tt class="py-lineno"> 69</tt>  <tt class="py-line"><tt class="py-docstring">        model used here, but any model could be substituted.</tt> </tt>
<a name="L70"></a><tt class="py-lineno"> 70</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L71"></a><tt class="py-lineno"> 71</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt><tt class="py-op">[</tt><tt class="py-name">new_sample</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-2" class="py-name" targets="Method BIP.SMC.condensation.Model.iterate()=BIP.SMC.condensation.Model-class.html#iterate"><a title="BIP.SMC.condensation.Model.iterate" class="py-name" href="#" onclick="return doclink('link-2', 'iterate', 'link-2');">iterate</a></tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">oldpos</tt><tt class="py-op">[</tt><tt class="py-name">old_sample</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">ProcessModel</tt><tt class="py-op">)</tt> </tt>
</div><a name="L72"></a><tt class="py-lineno"> 72</tt>  <tt class="py-line">         </tt>
<a name="Model.evaluateObservationDensity"></a><div id="Model.evaluateObservationDensity-def"><a name="L73"></a><tt class="py-lineno"> 73</tt> <a class="py-toggle" href="#" id="Model.evaluateObservationDensity-toggle" onclick="return toggle('Model.evaluateObservationDensity');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#evaluateObservationDensity">evaluateObservationDensity</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">new_sample</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.evaluateObservationDensity-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.evaluateObservationDensity-expanded"><a name="L74"></a><tt class="py-lineno"> 74</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L75"></a><tt class="py-lineno"> 75</tt>  <tt class="py-line"><tt class="py-docstring">        This routine evaluates the observation density</tt> </tt>
<a name="L76"></a><tt class="py-lineno"> 76</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L77"></a><tt class="py-lineno"> 77</tt>  <tt class="py-line"><tt class="py-docstring">        p(z_t|x_t = newpos[new_sample])</tt> </tt>
<a name="L78"></a><tt class="py-lineno"> 78</tt>  <tt class="py-line"><tt class="py-docstring"></tt> </tt>
<a name="L79"></a><tt class="py-lineno"> 79</tt>  <tt class="py-line"><tt class="py-docstring">        The observation model in this implementation is a simple mixture of</tt> </tt>
<a name="L80"></a><tt class="py-lineno"> 80</tt>  <tt class="py-line"><tt class="py-docstring">        Gaussians, where each simulated object is observed as a 1d position</tt> </tt>
<a name="L81"></a><tt class="py-lineno"> 81</tt>  <tt class="py-line"><tt class="py-docstring">        and measurement noise is represented as Gaussian. For a</tt> </tt>
<a name="L82"></a><tt class="py-lineno"> 82</tt>  <tt class="py-line"><tt class="py-docstring">        visual-tracking application, this routine would go and evaluate the</tt> </tt>
<a name="L83"></a><tt class="py-lineno"> 83</tt>  <tt class="py-line"><tt class="py-docstring">        likelihood that the object is present in the image at the position</tt> </tt>
<a name="L84"></a><tt class="py-lineno"> 84</tt>  <tt class="py-line"><tt class="py-docstring">        encoded by new_positions[new_sample]. </tt> </tt>
<a name="L85"></a><tt class="py-lineno"> 85</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L86"></a><tt class="py-lineno"> 86</tt>  <tt class="py-line">        <tt class="py-keyword">return</tt> <tt id="link-3" class="py-name" targets="Function BIP.SMC.condensation.evaluate_gaussian()=BIP.SMC.condensation-module.html#evaluate_gaussian"><a title="BIP.SMC.condensation.evaluate_gaussian" class="py-name" href="#" onclick="return doclink('link-3', 'evaluate_gaussian', 'link-3');">evaluate_gaussian</a></tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt><tt class="py-op">[</tt><tt class="py-name">new_sample</tt><tt class="py-op">]</tt><tt class="py-op">-</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">ObservationModel</tt> <tt class="py-op">)</tt> </tt>
</div><a name="Model.obtainObservations"></a><div id="Model.obtainObservations-def"><a name="L87"></a><tt class="py-lineno"> 87</tt> <a class="py-toggle" href="#" id="Model.obtainObservations-toggle" onclick="return toggle('Model.obtainObservations');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#obtainObservations">obtainObservations</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.obtainObservations-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.obtainObservations-expanded"><a name="L88"></a><tt class="py-lineno"> 88</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L89"></a><tt class="py-lineno"> 89</tt>  <tt class="py-line"><tt class="py-docstring">        In a real implementation, this routine would go and actually make</tt> </tt>
<a name="L90"></a><tt class="py-lineno"> 90</tt>  <tt class="py-line"><tt class="py-docstring">        measurements and store them in the data.meas structure. This</tt> </tt>
<a name="L91"></a><tt class="py-lineno"> 91</tt>  <tt class="py-line"><tt class="py-docstring">        simulation consists of an object moving around obeying a</tt> </tt>
<a name="L92"></a><tt class="py-lineno"> 92</tt>  <tt class="py-line"><tt class="py-docstring">        first-order auto-regressive process, and being observed with its</tt> </tt>
<a name="L93"></a><tt class="py-lineno"> 93</tt>  <tt class="py-line"><tt class="py-docstring">        true positions coorrupted by Gaussian measurement noise.</tt> </tt>
<a name="L94"></a><tt class="py-lineno"> 94</tt>  <tt class="py-line"><tt class="py-docstring">        Accordingly, this routine calculates the new simulated true and</tt> </tt>
<a name="L95"></a><tt class="py-lineno"> 95</tt>  <tt class="py-line"><tt class="py-docstring">        measured position of the object.</tt> </tt>
<a name="L96"></a><tt class="py-lineno"> 96</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L97"></a><tt class="py-lineno"> 97</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-4" class="py-name"><a title="BIP.SMC.condensation.Model.iterate" class="py-name" href="#" onclick="return doclink('link-4', 'iterate', 'link-2');">iterate</a></tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">SceneModel</tt><tt class="py-op">)</tt> </tt>
<a name="L98"></a><tt class="py-lineno"> 98</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">+</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt><tt class="py-op">.</tt><tt class="py-name">SceneModel</tt><tt class="py-op">[</tt><tt class="py-number">3</tt><tt class="py-op">]</tt><tt class="py-op">*</tt><tt class="py-name">normal</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
</div><a name="Model.display"></a><div id="Model.display-def"><a name="L99"></a><tt class="py-lineno"> 99</tt> <a class="py-toggle" href="#" id="Model.display-toggle" onclick="return toggle('Model.display');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Model-class.html#display">display</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">iteration</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Model.display-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Model.display-expanded"><a name="L100"></a><tt class="py-lineno">100</tt>  <tt class="py-line">        <tt class="py-name">aggregate</tt> <tt class="py-op">=</tt> <tt class="py-number">0.</tt> </tt>
<a name="L101"></a><tt class="py-lineno">101</tt>  <tt class="py-line">        <tt class="py-name">aggregate</tt> <tt class="py-op">=</tt>  <tt class="py-name">sum</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt><tt class="py-op">*</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">sample_weights</tt><tt class="py-op">)</tt><tt class="py-op">/</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">largest_cumulative_prob</tt> </tt>
<a name="L102"></a><tt class="py-lineno">102</tt>  <tt class="py-line">         </tt>
<a name="L103"></a><tt class="py-lineno">103</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">out</tt><tt class="py-op">[</tt><tt class="py-name">iteration</tt><tt class="py-op">,</tt> <tt class="py-op">:</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">data</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt><tt class="py-op">[</tt><tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">,</tt> <tt class="py-name">aggregate</tt><tt class="py-op">)</tt> </tt>
</div></div><a name="L104"></a><tt class="py-lineno">104</tt>  <tt class="py-line">        <tt class="py-comment">#print "%04d: Measured pos. % 3.4lf True pos. % 3.4lf Est. position % 3.4lf\n"%(iteration, self.data.meas[1], self.data.meas[0], aggregate)</tt> </tt>
<a name="L105"></a><tt class="py-lineno">105</tt>  <tt class="py-line">        <tt class="py-comment">#print "==&gt;Error: ",  self.data.meas[0]-aggregate</tt> </tt>
<a name="L106"></a><tt class="py-lineno">106</tt>  <tt class="py-line">             </tt>
<a name="GlobalData"></a><div id="GlobalData-def"><a name="L107"></a><tt class="py-lineno">107</tt> <a class="py-toggle" href="#" id="GlobalData-toggle" onclick="return toggle('GlobalData');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="BIP.SMC.condensation.GlobalData-class.html">GlobalData</a><tt class="py-op">:</tt> </tt>
</div><div id="GlobalData-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="GlobalData-expanded"><a name="GlobalData.__init__"></a><div id="GlobalData.__init__-def"><a name="L108"></a><tt class="py-lineno">108</tt> <a class="py-toggle" href="#" id="GlobalData.__init__-toggle" onclick="return toggle('GlobalData.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.GlobalData-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt>  <tt class="py-param">prior</tt><tt class="py-op">,</tt> <tt class="py-param">process</tt><tt class="py-op">,</tt> <tt class="py-param">scene</tt><tt class="py-op">,</tt> <tt class="py-param">observ</tt><tt class="py-op">,</tt> <tt class="py-param">nsam</tt><tt class="py-op">,</tt> <tt class="py-param">nit</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="GlobalData.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="GlobalData.__init__-expanded"><a name="L109"></a><tt class="py-lineno">109</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L110"></a><tt class="py-lineno">110</tt>  <tt class="py-line"><tt class="py-docstring">        Class to hold global data for the simulation</tt> </tt>
<a name="L111"></a><tt class="py-lineno">111</tt>  <tt class="py-line"><tt class="py-docstring">        </tt> </tt>
<a name="L112"></a><tt class="py-lineno">112</tt>  <tt class="py-line"><tt class="py-docstring">        :Parameters:</tt> </tt>
<a name="L113"></a><tt class="py-lineno">113</tt>  <tt class="py-line"><tt class="py-docstring">            -  `prior`: parameter tuple specifying the model of the prior distribution for the first step.</tt> </tt>
<a name="L114"></a><tt class="py-lineno">114</tt>  <tt class="py-line"><tt class="py-docstring">            -  `process`: the parameters specifying the process model. (mean,scaling, sigma)</tt> </tt>
<a name="L115"></a><tt class="py-lineno">115</tt>  <tt class="py-line"><tt class="py-docstring">            -  `scene`: parameters for the simlation model used to track the process (mean,scaling, sigma, sigma)</tt> </tt>
<a name="L116"></a><tt class="py-lineno">116</tt>  <tt class="py-line"><tt class="py-docstring">            -  `observ`: sigma of the observation model</tt> </tt>
<a name="L117"></a><tt class="py-lineno">117</tt>  <tt class="py-line"><tt class="py-docstring">            -  `nsam`: number of samples</tt> </tt>
<a name="L118"></a><tt class="py-lineno">118</tt>  <tt class="py-line"><tt class="py-docstring">            -  `nit`: number  of terations of the model</tt> </tt>
<a name="L119"></a><tt class="py-lineno">119</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L120"></a><tt class="py-lineno">120</tt>  <tt class="py-line">        <tt class="py-comment">#The prior distribution of the state is taken to be Gaussian with the parameters stored in this structure.</tt> </tt>
<a name="L121"></a><tt class="py-lineno">121</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">PriorModel</tt> <tt class="py-op">=</tt> <tt class="py-name">prior</tt> </tt>
<a name="L122"></a><tt class="py-lineno">122</tt>  <tt class="py-line">        <tt class="py-comment">#The parameters specifying the process model.</tt> </tt>
<a name="L123"></a><tt class="py-lineno">123</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">ProcessModel</tt> <tt class="py-op">=</tt> <tt class="py-name">process</tt> </tt>
<a name="L124"></a><tt class="py-lineno">124</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">SceneModel</tt> <tt class="py-op">=</tt> <tt class="py-name">scene</tt> </tt>
<a name="L125"></a><tt class="py-lineno">125</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">ObservationModel</tt> <tt class="py-op">=</tt> <tt class="py-name">observ</tt>         </tt>
<a name="L126"></a><tt class="py-lineno">126</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt> <tt class="py-op">=</tt> <tt class="py-name">nsam</tt> </tt>
<a name="L127"></a><tt class="py-lineno">127</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">niterations</tt> <tt class="py-op">=</tt> <tt class="py-name">nit</tt> </tt>
</div></div><a name="L128"></a><tt class="py-lineno">128</tt>  <tt class="py-line">         </tt>
<a name="IterationData"></a><div id="IterationData-def"><a name="L129"></a><tt class="py-lineno">129</tt> <a class="py-toggle" href="#" id="IterationData-toggle" onclick="return toggle('IterationData');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="BIP.SMC.condensation.IterationData-class.html">IterationData</a><tt class="py-op">(</tt><tt class="py-base-class">object</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="IterationData-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="IterationData-expanded"><a name="IterationData.__init__"></a><div id="IterationData.__init__-def"><a name="L130"></a><tt class="py-lineno">130</tt> <a class="py-toggle" href="#" id="IterationData.__init__-toggle" onclick="return toggle('IterationData.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.IterationData-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="IterationData.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="IterationData.__init__-expanded"><a name="L131"></a><tt class="py-lineno">131</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt> <tt class="py-op">=</tt> <tt class="py-number">0.</tt> </tt>
<a name="L132"></a><tt class="py-lineno">132</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">oldpos</tt> <tt class="py-op">=</tt> <tt class="py-number">0.</tt> </tt>
<a name="L133"></a><tt class="py-lineno">133</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">sample_weights</tt> <tt class="py-op">=</tt> <tt class="py-name">None</tt> </tt>
<a name="L134"></a><tt class="py-lineno">134</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">cumul_prob_array</tt> <tt class="py-op">=</tt> <tt class="py-name">None</tt> </tt>
<a name="L135"></a><tt class="py-lineno">135</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">largest_cumulative_prob</tt> <tt class="py-op">=</tt> <tt class="py-number">1.</tt> </tt>
<a name="L136"></a><tt class="py-lineno">136</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">meas</tt> <tt class="py-op">=</tt> <tt class="py-op">[</tt><tt class="py-number">0.</tt><tt class="py-op">,</tt> <tt class="py-number">0.</tt><tt class="py-op">]</tt> <tt class="py-comment">#(true,observed)</tt> </tt>
</div></div><a name="L137"></a><tt class="py-lineno">137</tt>  <tt class="py-line">         </tt>
<a name="Condensation"></a><div id="Condensation-def"><a name="L138"></a><tt class="py-lineno">138</tt> <a class="py-toggle" href="#" id="Condensation-toggle" onclick="return toggle('Condensation');">-</a><tt class="py-line"><tt class="py-keyword">class</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html">Condensation</a><tt class="py-op">(</tt><tt class="py-base-class">object</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="Condensation-expanded"><a name="Condensation.__init__"></a><div id="Condensation.__init__-def"><a name="L139"></a><tt class="py-lineno">139</tt> <a class="py-toggle" href="#" id="Condensation.__init__-toggle" onclick="return toggle('Condensation.__init__');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html#__init__">__init__</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt>  <tt class="py-param">model</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation.__init__-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Condensation.__init__-expanded"><a name="L140"></a><tt class="py-lineno">140</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt> <tt class="py-op">=</tt> <tt id="link-5" class="py-name" targets="Function BIP.Bayes.Melding.model()=BIP.Bayes.Melding-module.html#model"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-5', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">gdata</tt> </tt>
<a name="L141"></a><tt class="py-lineno">141</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt> <tt class="py-op">=</tt> <tt id="link-6" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-6', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">data</tt> </tt>
<a name="L142"></a><tt class="py-lineno">142</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-7" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-7', 'model', 'link-5');">model</a></tt> <tt class="py-op">=</tt> <tt id="link-8" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-8', 'model', 'link-5');">model</a></tt> </tt>
<a name="L143"></a><tt class="py-lineno">143</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt> <tt class="py-op">=</tt> <tt class="py-name">zeros</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">,</tt> <tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">dtype</tt> <tt class="py-op">=</tt> <tt class="py-name">float</tt><tt class="py-op">)</tt> </tt>
<a name="L144"></a><tt class="py-lineno">144</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">oldpos</tt> <tt class="py-op">=</tt> <tt class="py-name">zeros</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">,</tt> <tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">dtype</tt> <tt class="py-op">=</tt> <tt class="py-name">float</tt><tt class="py-op">)</tt> </tt>
<a name="L145"></a><tt class="py-lineno">145</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">sample_weights</tt> <tt class="py-op">=</tt> <tt class="py-name">zeros</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">,</tt> <tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">dtype</tt> <tt class="py-op">=</tt> <tt class="py-name">float</tt><tt class="py-op">)</tt> </tt>
<a name="L146"></a><tt class="py-lineno">146</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">cumul_prob_array</tt> <tt class="py-op">=</tt>  <tt class="py-name">zeros</tt><tt class="py-op">(</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">,</tt> <tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-name">dtype</tt> <tt class="py-op">=</tt> <tt class="py-name">float</tt><tt class="py-op">)</tt> </tt>
<a name="L147"></a><tt class="py-lineno">147</tt>  <tt class="py-line">         </tt>
<a name="L148"></a><tt class="py-lineno">148</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-9" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-9', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt id="link-10" class="py-name" targets="Method BIP.SMC.condensation.Model.setupPriorConditions()=BIP.SMC.condensation.Model-class.html#setupPriorConditions"><a title="BIP.SMC.condensation.Model.setupPriorConditions" class="py-name" href="#" onclick="return doclink('link-10', 'setupPriorConditions', 'link-10');">setupPriorConditions</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
</div><a name="L149"></a><tt class="py-lineno">149</tt>  <tt class="py-line">     </tt>
<a name="Condensation.pickBaseSample"></a><div id="Condensation.pickBaseSample-def"><a name="L150"></a><tt class="py-lineno">150</tt> <a class="py-toggle" href="#" id="Condensation.pickBaseSample-toggle" onclick="return toggle('Condensation.pickBaseSample');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html#pickBaseSample">pickBaseSample</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation.pickBaseSample-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Condensation.pickBaseSample-expanded"><a name="L151"></a><tt class="py-lineno">151</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L152"></a><tt class="py-lineno">152</tt>  <tt class="py-line"><tt class="py-docstring">        This is binary search using cumulative probabilities to pick a base</tt> </tt>
<a name="L153"></a><tt class="py-lineno">153</tt>  <tt class="py-line"><tt class="py-docstring">        sample. The use of this routine makes Condensation O(NlogN) where N</tt> </tt>
<a name="L154"></a><tt class="py-lineno">154</tt>  <tt class="py-line"><tt class="py-docstring">        is the number of samples. It is probably better to pick base</tt> </tt>
<a name="L155"></a><tt class="py-lineno">155</tt>  <tt class="py-line"><tt class="py-docstring">        samples deterministically, since then the algorithm is O(N) and</tt> </tt>
<a name="L156"></a><tt class="py-lineno">156</tt>  <tt class="py-line"><tt class="py-docstring">        probably marginally more efficient, but this routine is kept here</tt> </tt>
<a name="L157"></a><tt class="py-lineno">157</tt>  <tt class="py-line"><tt class="py-docstring">        for conceptual simplicity and because it maps better to the</tt> </tt>
<a name="L158"></a><tt class="py-lineno">158</tt>  <tt class="py-line"><tt class="py-docstring">        published literature.</tt> </tt>
<a name="L159"></a><tt class="py-lineno">159</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L160"></a><tt class="py-lineno">160</tt>  <tt class="py-line">        <tt class="py-name">choice</tt> <tt class="py-op">=</tt> <tt class="py-name">random</tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-op">*</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">largest_cumulative_prob</tt> </tt>
<a name="L161"></a><tt class="py-lineno">161</tt>  <tt class="py-line">        <tt class="py-name">low</tt> <tt class="py-op">=</tt> <tt class="py-number">0</tt> </tt>
<a name="L162"></a><tt class="py-lineno">162</tt>  <tt class="py-line">        <tt class="py-name">high</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt> </tt>
<a name="L163"></a><tt class="py-lineno">163</tt>  <tt class="py-line">         </tt>
<a name="L164"></a><tt class="py-lineno">164</tt>  <tt class="py-line">        <tt class="py-keyword">while</tt> <tt class="py-name">high</tt><tt class="py-op">&gt;</tt><tt class="py-op">(</tt><tt class="py-name">low</tt><tt class="py-op">+</tt><tt class="py-number">1</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L165"></a><tt class="py-lineno">165</tt>  <tt class="py-line">            <tt class="py-name">middle</tt> <tt class="py-op">=</tt> <tt class="py-op">(</tt><tt class="py-name">high</tt><tt class="py-op">+</tt><tt class="py-name">low</tt><tt class="py-op">)</tt><tt class="py-op">/</tt><tt class="py-number">2</tt> </tt>
<a name="L166"></a><tt class="py-lineno">166</tt>  <tt class="py-line">            <tt class="py-keyword">if</tt> <tt class="py-name">choice</tt> <tt class="py-op">&gt;</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">cumul_prob_array</tt><tt class="py-op">[</tt><tt class="py-name">middle</tt><tt class="py-op">]</tt><tt class="py-op">:</tt> </tt>
<a name="L167"></a><tt class="py-lineno">167</tt>  <tt class="py-line">                <tt class="py-name">low</tt> <tt class="py-op">=</tt> <tt class="py-name">middle</tt> </tt>
<a name="L168"></a><tt class="py-lineno">168</tt>  <tt class="py-line">            <tt class="py-keyword">else</tt><tt class="py-op">:</tt> </tt>
<a name="L169"></a><tt class="py-lineno">169</tt>  <tt class="py-line">                <tt class="py-name">high</tt> <tt class="py-op">=</tt> <tt class="py-name">middle</tt> </tt>
<a name="L170"></a><tt class="py-lineno">170</tt>  <tt class="py-line">                 </tt>
<a name="L171"></a><tt class="py-lineno">171</tt>  <tt class="py-line">        <tt class="py-keyword">return</tt> <tt class="py-name">low</tt> </tt>
</div><a name="L172"></a><tt class="py-lineno">172</tt>  <tt class="py-line">         </tt>
<a name="Condensation.predictNewBases"></a><div id="Condensation.predictNewBases-def"><a name="L173"></a><tt class="py-lineno">173</tt> <a class="py-toggle" href="#" id="Condensation.predictNewBases-toggle" onclick="return toggle('Condensation.predictNewBases');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html#predictNewBases">predictNewBases</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation.predictNewBases-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Condensation.predictNewBases-expanded"><a name="L174"></a><tt class="py-lineno">174</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L175"></a><tt class="py-lineno">175</tt>  <tt class="py-line"><tt class="py-docstring">        This method computes all of the new (unweighted) sample</tt> </tt>
<a name="L176"></a><tt class="py-lineno">176</tt>  <tt class="py-line"><tt class="py-docstring">        positions. For each sample, first a base is chosen, then the new</tt> </tt>
<a name="L177"></a><tt class="py-lineno">177</tt>  <tt class="py-line"><tt class="py-docstring">        sample position is computed by sampling from the prediction density</tt> </tt>
<a name="L178"></a><tt class="py-lineno">178</tt>  <tt class="py-line"><tt class="py-docstring">        p(x_t|x_t-1 = base). predict_sample_position is obviously</tt> </tt>
<a name="L179"></a><tt class="py-lineno">179</tt>  <tt class="py-line"><tt class="py-docstring">        model-dependent and is found in Model, but it can be</tt> </tt>
<a name="L180"></a><tt class="py-lineno">180</tt>  <tt class="py-line"><tt class="py-docstring">        replaced by any process model required.</tt> </tt>
<a name="L181"></a><tt class="py-lineno">181</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L182"></a><tt class="py-lineno">182</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">n</tt> <tt class="py-keyword">in</tt> <tt class="py-name">xrange</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L183"></a><tt class="py-lineno">183</tt>  <tt class="py-line">            <tt class="py-name">base</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-11" class="py-name" targets="Method BIP.SMC.condensation.Condensation.pickBaseSample()=BIP.SMC.condensation.Condensation-class.html#pickBaseSample"><a title="BIP.SMC.condensation.Condensation.pickBaseSample" class="py-name" href="#" onclick="return doclink('link-11', 'pickBaseSample', 'link-11');">pickBaseSample</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L184"></a><tt class="py-lineno">184</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-12" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-12', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt id="link-13" class="py-name" targets="Method BIP.SMC.condensation.Model.predictSamplePosition()=BIP.SMC.condensation.Model-class.html#predictSamplePosition"><a title="BIP.SMC.condensation.Model.predictSamplePosition" class="py-name" href="#" onclick="return doclink('link-13', 'predictSamplePosition', 'link-13');">predictSamplePosition</a></tt><tt class="py-op">(</tt><tt class="py-name">n</tt><tt class="py-op">,</tt> <tt class="py-name">base</tt><tt class="py-op">)</tt> </tt>
</div><a name="L185"></a><tt class="py-lineno">185</tt>  <tt class="py-line">     </tt>
<a name="Condensation.runFilter"></a><div id="Condensation.runFilter-def"><a name="L186"></a><tt class="py-lineno">186</tt> <a class="py-toggle" href="#" id="Condensation.runFilter-toggle" onclick="return toggle('Condensation.runFilter');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html#runFilter">runFilter</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation.runFilter-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Condensation.runFilter-expanded"><a name="L187"></a><tt class="py-lineno">187</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L188"></a><tt class="py-lineno">188</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L189"></a><tt class="py-lineno">189</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">i</tt> <tt class="py-keyword">in</tt> <tt class="py-name">xrange</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">niterations</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L190"></a><tt class="py-lineno">190</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-14" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-14', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt id="link-15" class="py-name" targets="Method BIP.SMC.condensation.Model.obtainObservations()=BIP.SMC.condensation.Model-class.html#obtainObservations"><a title="BIP.SMC.condensation.Model.obtainObservations" class="py-name" href="#" onclick="return doclink('link-15', 'obtainObservations', 'link-15');">obtainObservations</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-comment">#Go make necessary measurements</tt> </tt>
<a name="L191"></a><tt class="py-lineno">191</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-16" class="py-name" targets="Method BIP.SMC.condensation.Condensation.predictNewBases()=BIP.SMC.condensation.Condensation-class.html#predictNewBases"><a title="BIP.SMC.condensation.Condensation.predictNewBases" class="py-name" href="#" onclick="return doclink('link-16', 'predictNewBases', 'link-16');">predictNewBases</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt><tt class="py-comment">#Push previous state through process model</tt> </tt>
<a name="L192"></a><tt class="py-lineno">192</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-17" class="py-name" targets="Method BIP.SMC.condensation.Condensation.calculateBaseWeights()=BIP.SMC.condensation.Condensation-class.html#calculateBaseWeights"><a title="BIP.SMC.condensation.Condensation.calculateBaseWeights" class="py-name" href="#" onclick="return doclink('link-17', 'calculateBaseWeights', 'link-17');">calculateBaseWeights</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> <tt class="py-comment">#Apply Bayesian measurement weighting</tt> </tt>
<a name="L193"></a><tt class="py-lineno">193</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-18" class="py-name" targets="Method BIP.SMC.condensation.Condensation.updateAfterIterating()=BIP.SMC.condensation.Condensation-class.html#updateAfterIterating"><a title="BIP.SMC.condensation.Condensation.updateAfterIterating" class="py-name" href="#" onclick="return doclink('link-18', 'updateAfterIterating', 'link-18');">updateAfterIterating</a></tt><tt class="py-op">(</tt><tt class="py-name">i</tt><tt class="py-op">)</tt><tt class="py-comment">#</tt> </tt>
<a name="L194"></a><tt class="py-lineno">194</tt>  <tt class="py-line">        <tt class="py-comment">#print self.model.out.shape</tt> </tt>
<a name="L195"></a><tt class="py-lineno">195</tt>  <tt class="py-line">        <tt class="py-name">P</tt><tt class="py-op">.</tt><tt class="py-name">plot</tt><tt class="py-op">(</tt><tt class="py-name">nan_to_num</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-19" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-19', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">out</tt><tt class="py-op">[</tt><tt class="py-op">:</tt><tt class="py-op">,</tt> <tt class="py-number">0</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-string">'-^'</tt><tt class="py-op">)</tt> </tt>
<a name="L196"></a><tt class="py-lineno">196</tt>  <tt class="py-line">        <tt class="py-name">P</tt><tt class="py-op">.</tt><tt class="py-name">plot</tt><tt class="py-op">(</tt><tt class="py-name">nan_to_num</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-20" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-20', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">out</tt><tt class="py-op">[</tt><tt class="py-op">:</tt><tt class="py-op">,</tt> <tt class="py-number">1</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-string">'-^'</tt><tt class="py-op">)</tt> </tt>
<a name="L197"></a><tt class="py-lineno">197</tt>  <tt class="py-line">        <tt class="py-name">P</tt><tt class="py-op">.</tt><tt class="py-name">plot</tt><tt class="py-op">(</tt><tt class="py-name">nan_to_num</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-21" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-21', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt class="py-name">out</tt><tt class="py-op">[</tt><tt class="py-op">:</tt><tt class="py-op">,</tt> <tt class="py-number">2</tt><tt class="py-op">]</tt><tt class="py-op">)</tt><tt class="py-op">,</tt> <tt class="py-string">'-^'</tt><tt class="py-op">)</tt> </tt>
<a name="L198"></a><tt class="py-lineno">198</tt>  <tt class="py-line">        <tt class="py-name">P</tt><tt class="py-op">.</tt><tt class="py-name">legend</tt><tt class="py-op">(</tt><tt class="py-op">[</tt><tt class="py-string">'Measured'</tt><tt class="py-op">,</tt> <tt class="py-string">'True'</tt><tt class="py-op">,</tt> <tt class="py-string">'Estimated'</tt><tt class="py-op">]</tt><tt class="py-op">)</tt> </tt>
<a name="L199"></a><tt class="py-lineno">199</tt>  <tt class="py-line">        <tt class="py-name">P</tt><tt class="py-op">.</tt><tt class="py-name">show</tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
</div><a name="L200"></a><tt class="py-lineno">200</tt>  <tt class="py-line">     </tt>
<a name="Condensation.calculateBaseWeights"></a><div id="Condensation.calculateBaseWeights-def"><a name="L201"></a><tt class="py-lineno">201</tt> <a class="py-toggle" href="#" id="Condensation.calculateBaseWeights-toggle" onclick="return toggle('Condensation.calculateBaseWeights');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html#calculateBaseWeights">calculateBaseWeights</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation.calculateBaseWeights-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Condensation.calculateBaseWeights-expanded"><a name="L202"></a><tt class="py-lineno">202</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L203"></a><tt class="py-lineno">203</tt>  <tt class="py-line"><tt class="py-docstring">        Once all the unweighted sample positions have been computed using</tt> </tt>
<a name="L204"></a><tt class="py-lineno">204</tt>  <tt class="py-line"><tt class="py-docstring">        predict_new_bases, this routine computes the weights by evaluating</tt> </tt>
<a name="L205"></a><tt class="py-lineno">205</tt>  <tt class="py-line"><tt class="py-docstring">        the observation density at each of the positions. Cumulative</tt> </tt>
<a name="L206"></a><tt class="py-lineno">206</tt>  <tt class="py-line"><tt class="py-docstring">        probabilities are also computed at the same time, to permit an</tt> </tt>
<a name="L207"></a><tt class="py-lineno">207</tt>  <tt class="py-line"><tt class="py-docstring">        efficient implementation of pick_base_sample using binary</tt> </tt>
<a name="L208"></a><tt class="py-lineno">208</tt>  <tt class="py-line"><tt class="py-docstring">        search. evaluate_observation_density is obviously model-dependent</tt> </tt>
<a name="L209"></a><tt class="py-lineno">209</tt>  <tt class="py-line"><tt class="py-docstring">        and is found in the Model class, but it can be replaced by any</tt> </tt>
<a name="L210"></a><tt class="py-lineno">210</tt>  <tt class="py-line"><tt class="py-docstring">        observation model required.</tt> </tt>
<a name="L211"></a><tt class="py-lineno">211</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L212"></a><tt class="py-lineno">212</tt>  <tt class="py-line">        <tt class="py-name">cumul_total</tt> <tt class="py-op">=</tt> <tt class="py-number">0.0</tt> </tt>
<a name="L213"></a><tt class="py-lineno">213</tt>  <tt class="py-line">        <tt class="py-keyword">for</tt> <tt class="py-name">n</tt> <tt class="py-keyword">in</tt> <tt class="py-name">xrange</tt><tt class="py-op">(</tt><tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">globaldata</tt><tt class="py-op">.</tt><tt class="py-name">nsamples</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
<a name="L214"></a><tt class="py-lineno">214</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">sample_weights</tt><tt class="py-op">[</tt><tt class="py-name">n</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-22" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-22', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt id="link-23" class="py-name" targets="Method BIP.SMC.condensation.Model.evaluateObservationDensity()=BIP.SMC.condensation.Model-class.html#evaluateObservationDensity"><a title="BIP.SMC.condensation.Model.evaluateObservationDensity" class="py-name" href="#" onclick="return doclink('link-23', 'evaluateObservationDensity', 'link-23');">evaluateObservationDensity</a></tt><tt class="py-op">(</tt><tt class="py-name">n</tt><tt class="py-op">)</tt> </tt>
<a name="L215"></a><tt class="py-lineno">215</tt>  <tt class="py-line">            <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">cumul_prob_array</tt><tt class="py-op">[</tt><tt class="py-name">n</tt><tt class="py-op">]</tt> <tt class="py-op">=</tt> <tt class="py-name">cumul_total</tt><tt class="py-op">;</tt> </tt>
<a name="L216"></a><tt class="py-lineno">216</tt>  <tt class="py-line">            <tt class="py-name">cumul_total</tt> <tt class="py-op">+=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">sample_weights</tt><tt class="py-op">[</tt><tt class="py-name">n</tt><tt class="py-op">]</tt><tt class="py-op">;</tt> </tt>
<a name="L217"></a><tt class="py-lineno">217</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">largest_cumulative_prob</tt> <tt class="py-op">=</tt> <tt class="py-name">cumul_total</tt> </tt>
</div><a name="L218"></a><tt class="py-lineno">218</tt>  <tt class="py-line"> </tt>
<a name="Condensation.updateAfterIterating"></a><div id="Condensation.updateAfterIterating-def"><a name="L219"></a><tt class="py-lineno">219</tt> <a class="py-toggle" href="#" id="Condensation.updateAfterIterating-toggle" onclick="return toggle('Condensation.updateAfterIterating');">-</a><tt class="py-line">    <tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation.Condensation-class.html#updateAfterIterating">updateAfterIterating</a><tt class="py-op">(</tt><tt class="py-param">self</tt><tt class="py-op">,</tt> <tt class="py-param">iteration</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="Condensation.updateAfterIterating-collapsed" style="display:none;" pad="+++" indent="++++++++"></div><div id="Condensation.updateAfterIterating-expanded"><a name="L220"></a><tt class="py-lineno">220</tt>  <tt class="py-line">        <tt class="py-docstring">'''</tt> </tt>
<a name="L221"></a><tt class="py-lineno">221</tt>  <tt class="py-line"><tt class="py-docstring">        Go and output the estimate for this iteration (which is a</tt> </tt>
<a name="L222"></a><tt class="py-lineno">222</tt>  <tt class="py-line"><tt class="py-docstring">        model-dependent routine found in Model) and then swap</tt> </tt>
<a name="L223"></a><tt class="py-lineno">223</tt>  <tt class="py-line"><tt class="py-docstring">        over the arrays ready for the next iteration.</tt> </tt>
<a name="L224"></a><tt class="py-lineno">224</tt>  <tt class="py-line"><tt class="py-docstring">        '''</tt> </tt>
<a name="L225"></a><tt class="py-lineno">225</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt id="link-24" class="py-name"><a title="BIP.Bayes.Melding.model" class="py-name" href="#" onclick="return doclink('link-24', 'model', 'link-5');">model</a></tt><tt class="py-op">.</tt><tt id="link-25" class="py-name" targets="Method BIP.SMC.condensation.Model.display()=BIP.SMC.condensation.Model-class.html#display"><a title="BIP.SMC.condensation.Model.display" class="py-name" href="#" onclick="return doclink('link-25', 'display', 'link-25');">display</a></tt><tt class="py-op">(</tt><tt class="py-name">iteration</tt><tt class="py-op">)</tt> </tt>
<a name="L226"></a><tt class="py-lineno">226</tt>  <tt class="py-line">        <tt class="py-name">temp</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt> </tt>
<a name="L227"></a><tt class="py-lineno">227</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">newpos</tt> <tt class="py-op">=</tt> <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">oldpos</tt> </tt>
<a name="L228"></a><tt class="py-lineno">228</tt>  <tt class="py-line">        <tt class="py-name">self</tt><tt class="py-op">.</tt><tt class="py-name">iterdata</tt><tt class="py-op">.</tt><tt class="py-name">oldpos</tt> <tt class="py-op">=</tt> <tt class="py-name">temp</tt> </tt>
</div></div><a name="L229"></a><tt class="py-lineno">229</tt>  <tt class="py-line">         </tt>
<a name="evaluate_gaussian"></a><div id="evaluate_gaussian-def"><a name="L230"></a><tt class="py-lineno">230</tt> <a class="py-toggle" href="#" id="evaluate_gaussian-toggle" onclick="return toggle('evaluate_gaussian');">-</a><tt class="py-line"><tt class="py-keyword">def</tt> <a class="py-def-name" href="BIP.SMC.condensation-module.html#evaluate_gaussian">evaluate_gaussian</a><tt class="py-op">(</tt><tt class="py-param">val</tt><tt class="py-op">,</tt> <tt class="py-param">sigma</tt><tt class="py-op">)</tt><tt class="py-op">:</tt> </tt>
</div><div id="evaluate_gaussian-collapsed" style="display:none;" pad="+++" indent="++++"></div><div id="evaluate_gaussian-expanded"><a name="L231"></a><tt class="py-lineno">231</tt>  <tt class="py-line">    <tt class="py-keyword">return</tt> <tt class="py-number">1.0</tt><tt class="py-op">/</tt><tt class="py-op">(</tt><tt class="py-name">sqrt</tt><tt class="py-op">(</tt><tt class="py-number">2.0</tt><tt class="py-op">*</tt><tt class="py-name">pi</tt><tt class="py-op">)</tt> <tt class="py-op">*</tt> <tt class="py-name">sigma</tt><tt class="py-op">)</tt> <tt class="py-op">*</tt> <tt class="py-name">exp</tt><tt class="py-op">(</tt><tt class="py-op">-</tt><tt class="py-number">0.5</tt> <tt class="py-op">*</tt> <tt class="py-op">(</tt><tt class="py-name">val</tt><tt class="py-op">*</tt><tt class="py-name">val</tt> <tt class="py-op">/</tt> <tt class="py-op">(</tt><tt class="py-name">sigma</tt><tt class="py-op">*</tt><tt class="py-name">sigma</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">)</tt><tt class="py-op">;</tt> </tt>
</div><a name="L232"></a><tt class="py-lineno">232</tt>  <tt class="py-line"> </tt>
<a name="L233"></a><tt class="py-lineno">233</tt>  <tt class="py-line"> </tt>
<a name="L234"></a><tt class="py-lineno">234</tt>  <tt class="py-line">     </tt>
<a name="L235"></a><tt class="py-lineno">235</tt>  <tt class="py-line">     </tt>
<a name="L236"></a><tt class="py-lineno">236</tt>  <tt class="py-line"><tt class="py-name">normal</tt> </tt>
<a name="L237"></a><tt class="py-lineno">237</tt>  <tt class="py-line">     </tt>
<a name="L238"></a><tt class="py-lineno">238</tt>  <tt class="py-line"><tt class="py-keyword">if</tt> <tt class="py-name">__name__</tt><tt class="py-op">==</tt><tt class="py-string">"__main__"</tt><tt class="py-op">:</tt> </tt>
<a name="L239"></a><tt class="py-lineno">239</tt>  <tt class="py-line">    <tt id="link-26" class="py-name" targets="Variable BIP.SDE.example.M=BIP.SDE.example-module.html#M"><a title="BIP.SDE.example.M" class="py-name" href="#" onclick="return doclink('link-26', 'M', 'link-26');">M</a></tt> <tt class="py-op">=</tt> <tt id="link-27" class="py-name" targets="Class BIP.SDE.gillespie.Model=BIP.SDE.gillespie.Model-class.html,Class BIP.SMC.condensation.Model=BIP.SMC.condensation.Model-class.html"><a title="BIP.SDE.gillespie.Model
BIP.SMC.condensation.Model" class="py-name" href="#" onclick="return doclink('link-27', 'Model', 'link-27');">Model</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L240"></a><tt class="py-lineno">240</tt>  <tt class="py-line">    <tt class="py-name">C</tt> <tt class="py-op">=</tt> <tt id="link-28" class="py-name" targets="Class BIP.SMC.condensation.Condensation=BIP.SMC.condensation.Condensation-class.html"><a title="BIP.SMC.condensation.Condensation" class="py-name" href="#" onclick="return doclink('link-28', 'Condensation', 'link-28');">Condensation</a></tt><tt class="py-op">(</tt><tt id="link-29" class="py-name"><a title="BIP.SDE.example.M" class="py-name" href="#" onclick="return doclink('link-29', 'M', 'link-26');">M</a></tt><tt class="py-op">)</tt> </tt>
<a name="L241"></a><tt class="py-lineno">241</tt>  <tt class="py-line">    <tt class="py-name">C</tt><tt class="py-op">.</tt><tt id="link-30" class="py-name" targets="Method BIP.SMC.condensation.Condensation.runFilter()=BIP.SMC.condensation.Condensation-class.html#runFilter"><a title="BIP.SMC.condensation.Condensation.runFilter" class="py-name" href="#" onclick="return doclink('link-30', 'runFilter', 'link-30');">runFilter</a></tt><tt class="py-op">(</tt><tt class="py-op">)</tt> </tt>
<a name="L242"></a><tt class="py-lineno">242</tt>  <tt class="py-line"> </tt><script type="text/javascript">
<!--
expandto(location.href);
// -->
</script>
</pre>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="BIP-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://code.google.com/p/bayesian-inference">Project Homepage</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Thu Jan  7 10:45:34 2010
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
